//房源panal
Ext.define('customer.subscribe.view.estatePanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var row_1={
				id:"isPaydepositTrue",
				fieldDefaults: {
			            labelAlign: 'top',
			            msgTarget: 'side'
			    },
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
			    border: false,
				layout: 'hbox',
				items: [{
		            items: [{
		            	xtype:'container',
		            	layout:'hbox',
		            	border:true,
		            	items:[{
		            		xtype:'displayfield',
			                fieldLabel: '房号',
			                name: 'combinedName'
			                }]
		                
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '建筑面积(㎡)',
		                anchor: '80%',
		                name: 'buildingArea'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '分摊面积(㎡)',
		                anchor: '80%',
		                name: 'sharingArea'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '折前单价(元)',
		                anchor: '80%',
		                name: 'unitPrice',
		                readOnly:true
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '房源状态',
		                anchor: '80%',
		                name: 'roomStatusName'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '套内面积(㎡)',
		                anchor: '80%',
		                name: 'innerArea'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '计价面积(㎡)',
		                anchor: '80%',
		                name: 'calculateArea'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '折前总价(元)',
		                anchor: '80%',
		                name: 'totalPrice'
		            }]
		        }]
	    };
		var items=[row_1];
		var initConfigg = {
				id:'estatePanel',
				title:"房产信息",
				bodyStyle: 'padding:5px 5px 0',
		        minHeight: 200,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});

Ext.define('customer.subscribe.view.customerPanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var row_1={
				fieldDefaults: {
			            labelAlign: 'top',
			            msgTarget: 'side'
			    },
			    border: false,
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
				layout: 'hbox',
				items: [{
		            items: [{
		            	xtype:'container',
		            	layout:'hbox',
		            	anchor: '80%',
		            	items:[{
		            		xtype:'displayfield',
			                fieldLabel: '客户姓名',
			                anchor: '80%',
			                name: 'customerName'
			                }]
		                
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '证件类型',
		                anchor: '80%',
		                name: 'peDocumentTypeName'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '证件号码',
		                anchor: '80%',
		                name: 'peDocumentNumber'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '联系电话',
		                anchor: '80%',
		                name: 'customerPhone'
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '邮寄地址',
		                anchor: '80%',
		                name: 'peContactAddress'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '邮编',
		                anchor: '80%',
		                name: 'pePostcode'
		            },{
		                xtype:'displayfield',
		                fieldLabel: '客户来源',
		                anchor: '80%',
		                name: 'customerOriginName'
		            }]
		        }]
	    };
		var documentTypeStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'name'],
		    data :documentType
		});
		var gridStore = Ext.create('Ext.data.Store', {
			id:'familyMemberGridStore',
		    fields: ["memberName","documentType","documentNumber","mobile"],
		    //是否在服务端排序 （true的话，在客户端就不能排序）
		    remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/customer/familyMember/findList',
		        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
		            type: 'json', //返回数据类型为json格式
		            root: 'obj'
		        },
		        actionMethods: {read: 'POST'},
	            extraParams:{
	            	buyId:subscribeId,
	            	memberType:'4'
	            }
		    },
		    autoLoad:true
		});
		var columns = [{
			text : "姓名",
			dataIndex : 'memberName',
			width:'20%' 
		}, {
			text : "证件类型",
			dataIndex : 'documentType',
			width:'20%',
            renderer: function(value,metadata,record){  
                var index = documentTypeStore.find('value',value);  
                if(index!=-1){  
                    return documentTypeStore.getAt(index).data.name;  
                }  
                return value;  
            }
		}, {
			text : "证件号码",
			dataIndex : 'documentNumber',
			width:'20%'
		},{
			text : "手机号码",
			dataIndex : 'mobile',
			width:'20%'
			
		}];
		var row_2=Ext.create('Ext.grid.Panel', {
			id:'familyMemberGridPanel',
			region : 'south',
			title:"共同购房者",
			minHeight:200,
			columnLines: true,
	        viewConfig: {
	            stripeRows: true
	        },
			store : gridStore,
			columns : columns
		});
		var items=[row_1,row_2];
		var initConfigg = {
				id:'customerPanel',
		        title:"客户信息",
		        minHeight:320,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});
Ext.define('customer.signed.view.signedPanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var me=this;
		var contactTypesStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'name'],
		    data:contactTypes
		});
		var row_1={
				fieldDefaults: {
			            labelAlign: 'left',
			            msgTarget: 'side'
			    },
			    border: false,
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
				layout: 'hbox',
				items: [{
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '签约ID',
		                name:'id',
		                hidden: true
		            },{
		                xtype:'displayfield',
		                fieldLabel: '合同编号',
		                name:'signCode'
		            },{
                        xtype:'displayfield',
                        fieldLabel: '合同签署日期',
                        name:'checkDate',
                        id:'checkDate'
                    },{
                        xtype: 'displayfield',
                        fieldLabel: '业务归属日期',
                        name:'attributionDate',
                        id:'attributionDate'
                    },{
		                xtype:'displayfield',
		                fieldLabel: '折前单价(元)',
		                name:'conversionPrice'
		            }, {
		                xtype:'displayfield',
		                fieldLabel: '业务状态',
		                name:'businessStatus',
		                renderer : function(val) {
		                	 if (val == 1) {
		                         return '签约';
		                     } else if (val == 2) {
		                         return '已退房';
		                     }else if (val == 3) {
		                         return '已换房';
		                     }else if (val == 4) {
		                         return '已更名';
		                     }
		                     return val;
		                }
		            }]
		        }, {
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '合同类型',
		                name: 'contactTypeName'
		            },{
                        xtype:'displayfield',
                        fieldLabel: '交房日期',
                        name:'handoverDate',
                        id:'handoverDate'
                    },{
                        xtype:'displayfield',
                        fieldLabel: '是否工抵贷',
                        name: 'isFlag'
                    },{
		                xtype:'displayfield',
		                fieldLabel: '折前总价(元)',
		                name: 'lumpsumPrice'
		            }]
		        }]
	    };
		var row_2={
				fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side'
		        },
		        collapsible: true,
				xtype: 'fieldset',
                title:'折扣信息',
                items:[{
                	xtype: 'displayfield',
                	fieldLabel: '折后总价(元)',
                    name: 'discountAfterPrice'
                },{
                	xtype: 'displayfield',
                	fieldLabel: '折扣金额(元)',
                    name: 'discountAmount'
                },{
	            	xtype:'container',
	            	layout:'hbox',
	            	anchor: '80%',
	            	items:[{
	            		xtype: 'displayfield',
		                fieldLabel:'折扣比例(%)',
		                name:'discountRate'
		                
	            	}]
	                
	            },{
                	xtype: 'displayfield',
                	fieldLabel: '折扣说明',
                	anchor: '50%',
                    name: 'discountNote'
                }]
	    };
		var row_3={
				fieldDefaults: {
			            labelAlign: 'left',
			            msgTarget: 'side'
			    },
			    border: false,
			    defaults: {
			            border: false,
			            xtype: 'panel',
			            flex: 1,
			            layout: 'anchor'
			    },
				layout: 'hbox',
				items: [{
		            items: [{
		                xtype:'displayfield',
		                fieldLabel: '签约单价(元)',
		                name: 'signPrice',
		                renderer: Ext.util.Format.numberRenderer('0,000.00')
		            }]
		        }, {
		            items: [{
		            	xtype: 'displayfield',
		                fieldLabel: '合同总金额(元)',
		                name:'signAmount',
		                id:'signAmount',
		                renderer: Ext.util.Format.numberRenderer('0,000.00')
		            }]
		        }, {
		            items: [{
		            	xtype: 'displayfield',
		                fieldLabel: '房款金额(元)',
		                name:'roomTotalAmount',
		                id:'roomTotalAmount',	
		                renderer: Ext.util.Format.numberRenderer('0,000.00')
		            }]
		        }]
	    };
		var dealCategoryStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'text'],
		    data : [
               // {"value":"1", "text":"推荐成交"},
		        {"value":"2", "text":"单独成交"},
		        {"value":"3", "text":"共同成交"}
		    ]
		});
		var paymentMethodsStore = Ext.create('Ext.data.Store', {
		    fields: ['value', 'name'],
		    data :paymentMethods
		});
		
    	Ext.define('needReceiveRecord', { 
            extend: 'Ext.data.Model', 
            fields: ["receivablesFunds","totalReceivablesAmount","unpaidAmount","receiveAmount","receivablesDate","orgName","agentName","businessFlag","housingFundType","loanLife"]
	    });
		
		var gridStore = Ext.create('Ext.data.Store', {
			model:'needReceiveRecord',
			remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/marketing/needReceiveRefund/findList',
		        reader: {  
		            type: 'json', //返回数据类型为json格式
		            root: 'obj'
		        },
		        actionMethods: {read: 'POST'},
	            extraParams:{
	            	relationId:subscribeId,
	            	businessType:'2'
	            }
		    },
		    autoLoad:true
		});
		var columns = [{
			text : "款项",
			dataIndex : 'receivablesFunds',
			width:'10%'
		}, {
			text : "应收金额(元)",
			dataIndex : 'totalReceivablesAmount',
			width:'10%'
		},{
			text : "已收金额(元)",
			dataIndex : 'receiveAmount',
			width:'10%'
		}, {
			text : "应收日期",
			dataIndex : 'receivablesDate',
			width:'10%',
            renderer: function(value,metadata,record){  
                if(value!=null && value!=""){
                	return Ext.Date.format(new Date(value),'Y-m-d');
                }
                return value;
            }
		}, {
			text : "银行单位",
			dataIndex : 'orgName',
			width:'10%'
		}, {
			text : "经办人",
			dataIndex : 'agentName',
			width:'10%'
		}, {
			text : "未收金额(元)",
			dataIndex : 'unpaidAmount',
			width:'10%'
		}, {
			text : "贷款年限",
			dataIndex : 'loanLife',
			width:'10%'
		}];
		var row_7=Ext.create('Ext.grid.Panel', {
			id:'receivableAmountPanel',
			title:"应收款",
			minHeight:200,
			columnLines: true,
	        viewConfig: {
	            stripeRows: true
	        },
			store : gridStore,
			columns : columns
		});
		//上传图片展示框
		var showImgPanel = Ext.create('Ext.Panel', {
			id:'signedFileFormPanel',
	        width:'80%',
	       	height:120,
	        frame: true,
	        collapsible: false,
	      	autoScroll:true,
	      	bodyStyle: 'overflow-y:hidden; overflow-x:auto',
	        layout: "hbox"
	    });
		
		var signedFileForm = Ext.create("Ext.form.Panel", {
			border: false,
			items: [{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	fieldLabel: "合同扫描附件",
	        	id: "signedFileForm",
	        	hidden: true,
	        	items: [showImgPanel]
	        }]
		});
		var row_4 = {
			fieldDefaults: {
	            labelAlign: 'left',
	            msgTarget: 'side'
		    },
		    border: false,
		    defaults: {
		            border: false,
		            xtype: 'panel',
		            flex: 1,
		            layout: 'anchor'
		    },
			layout: 'hbox',
			items: [{
		        	xtype: 'displayfield',
		            fieldLabel: '支付方式',
		            name:'paymentMethodName'
		        },{
		        	xtype: 'displayfield',
		            fieldLabel: '首付分期支付方式',
		            name:'decorationPaymentMethodName'
		        },{
		        	items: [{
		            	xtype: 'displayfield',
		                fieldLabel: '首付分期金额(元)',
		                name:'decorationMoneyAmount',
		                id:'decorationMoneyAmount',
		          		renderer: Ext.util.Format.numberRenderer('0,000.00')
		            }]
		        },{
		        	xtype: 'displayfield',
		            fieldLabel: '支付方式',
		            name:'paymentMethod',
		            hidden: true
			}]
		};
		var items=[row_1,row_2,row_3,row_4,row_7,//signedFileForm,
        {
        	xtype: 'displayfield',
        	hidden:payDepositOff,
            fieldLabel: '已收认筹金(元)',
            name:'payAepositAmount'
        },{
        	xtype: 'displayfield',
            fieldLabel: '已收认购金(元)',
            name:'subscribeAmount'
        },{
        	xtype: 'displayfield',
            fieldLabel: '备注',
            anchor: '50%',
            name:'remark'
        },{
        	xtype: 'displayfield',
            fieldLabel: '成交类别',
            name:'dealCategoryName',
            renderer:function(value){
            	if(value=="推荐成交"){
        			Ext.getCmp("referenceName").show();
        			//Ext.getCmp("referenceProportion").show();
            		return value;
            	}
            	return value;
            }
        },{xtype:"displayfield", name:"referenceId", hidden:true},
            		{xtype:"displayfield", id:'referenceName', fieldLabel:'推荐人姓名', hidden:true, name:"referenceName"},
            		{xtype:"displayfield", id:'referenceProportion', fieldLabel:'推荐人比例', hidden:true, name:"referenceProportion",maxLength:3,regex:/^[1-9]\d*$/}
        ,{
        	xtype: 'displayfield',
            fieldLabel: '经纪人',
            name:'userName',
            value:userName
        },{
        	xtype: 'displayfield',
            fieldLabel: '签约日期',
            name:'didTime',
            id:'didTime'
        },{
                xtype: 'displayfield',
                fieldLabel: '流程审批人',
                name:'nodeName',
                id:'nodeName'
            }];
		var initConfigg = {
				id:"subscribeInfoForm",
				name: "subscribeInfoForm",
		        title:"签约信息",
		        minHeight:200,
		        fieldDefaults: {
		            labelAlign: 'left',
		            msgTarget: 'side',
		            margin: 5
		        },
		        items:items
		};
		Ext.apply(this,initConfigg);
		this.callParent(arguments);
	}
});


Ext.define('customer.subscribe.viewPanel',{
	extend : 'Ext.form.Panel',
	initComponent:function(){
		var estatePanel= Ext.create("customer.subscribe.view.estatePanel");
		var customerPanel= Ext.create("customer.subscribe.view.customerPanel");
		var payDepositPanel= Ext.create("customer.signed.view.signedPanel");
		//审批历史
		var selectWindow=Ext.create('component.proces.history',{
			 processInstanceId: procInstId,
			 contextPath:contextPath
    	});
		//未提交状态，无审核记录，不显示
		if("0" == auditStatus){
			selectWindow = null;
		}
		var fileAttachment = Ext.create('component.file.attachment',{
            objectId: subscribeId,
            imageType: "3",
            contextPath:contextPath
       });
		var defaultConfigg = {
				buttonAlign:'center',
				defaults: {  
		            split: true,    //有分割线  
		            collapsible: true  //可以折叠
		        },
		        
		        renderTo:Ext.getBody(),
				items:[estatePanel,customerPanel,payDepositPanel,selectWindow,fileAttachment],
				tbar:[
					{hidden:marketingSignedReceivables,xtype: "button", text: "收款",iconCls:'icon-accept',
					    handler:function(){
					    	 var tabKey = "sig_pay"+subscribeId;
						     var url = "/marketing/proceeds/signed?id="+subscribeId+"&1=1";
						     business(subscribeId,tabKey,"签约收款",url);
				        },id:'proceedsBut'
				        
					},{hidden:marketingDeferredPayment,xtype: "button", text: "延期收款",iconCls:'icon-accept',
					    handler:function(){
					    	Ext.Ajax.request({
				                   url: contextPath+'/customer/signed/toDeferredPaymentIS',
				                   params: {
				                	   signedId: subscribeId
				                   },
				                   method: 'POST',
				                   success: function(response){
				                       var text = Ext.decode(response.responseText);
				                       //res: 0,失败(基本为异常) 1,成功
				                       //resMsg: 0(正常发起), 1(存在审核中内容,不允许发起), 2(存在通过的延期,提醒后发起)
				                       if(text.res=="0"){
				                    	   Ext.MessageBox.alert("系统提示",text.resMsg);
				                    	   return;
				                       }else if(text.res=="1"){
				                    	   if(text.resMsg == "1"){
				                    		   Ext.MessageBox.alert('提示', '已发起延期收款申请，请等待审核。您可在【待办事项-我的发起】中查看审批进度。');
				                    		   return;
				                    	   }else if(text.resMsg == "2"){
				                    		   var obj = text.obj;
				                    		   Ext.MessageBox.confirm(
			                    				   "系统提示",
			                    				   "<h5>审批过同类流程。</h5><div>" + obj + "</div><h4 style='color: red;'>是否继续申请?</h4>",
			                    				    function(btn){
				                    			   		if("yes" == btn){
				                    			   			var tabKey = "def_"+subscribeId;
				               			     	       	 	var url = "/router/marketing/receivables/deferredPayment?signedId="+subscribeId+"&1=1";
				               			     	       	 	business(subscribeId,tabKey,"延期收款",url);
				                    			   		}
	                    		   					},
	                		   						this
	                		   					);
				                    	   }else{
				                    		   var tabKey = "def_"+subscribeId;
				  			     	       	 	var url = "/router/marketing/receivables/deferredPayment?signedId="+subscribeId+"&1=1";
				  			     	       	 	business(subscribeId,tabKey,"延期收款",url);
				                    	   }
					     	       	   }else{
					     	       		   var tabKey = "def_"+subscribeId;
					     	       		   var url = "/router/marketing/receivables/deferredPayment?signedId="+subscribeId+"&1=1";
					     	       		   business(subscribeId,tabKey,"延期收款",url);
					     	       		   
					     	       	   }
				                   }
				            });
				        } 
					},
		        	{hidden:marketingSignedcontractChange,xtype: "button", text: "合同变更", id:'contactChange', iconCls:'icon-accept',
						handler:function(){
	           		 	    var selectWindow=Ext.create('component.signedcontactchange.selectWindow',{
	           		 	    	callbackFun: function (record) {
	           		 	        },
	           		 	    	paymentMethod: payDepositPanel.getForm().findField("paymentMethod").getValue(),
	           		 	    	signAmount: payDepositPanel.getForm().findField("signAmount").getValue(),
	           		 	    	signedId: payDepositPanel.getForm().findField("id").getValue(),
	           		 	    	modal: true
	           		 	    });
	           		 	    selectWindow.show();
				        }
					},
		        	{hidden:marketingSignedChangeHouse,xtype: "button", text: "换房", id:'roomChange', iconCls:'icon-accept',
						handler:function(){
							if(huanStatus=="1"){
								Ext.Msg.alert('提示', '换房审核中，请勿重复提交!');
								return;
							}
							 var signedId = payDepositPanel.getForm().findField("id").getValue();
							 var tabKey = "cgr_"+signedId;
			     	       	 var url = "/marketing/changeroom/signedChangeHouse/?signedId=" + signedId+"&1=1";
			     	       	 business(signedId,tabKey,"签约换房",url);
				        }
					},
		        	{hidden:marketingSignedTuiHouse,xtype: "button", text: "退房", iconCls:'icon-exclamation',
						handler:function(){
							var signedId = payDepositPanel.getForm().findField("id").getValue();
							var tabKey = "cto"+signedId;
			     	       	var url = "/customer/signed/toCheckOut?signedId="+signedId+"&1=1";
			     	        business(signedId,tabKey,"签约退房",url);
				        }
					},
		        	{hidden:marketingSigendGengName,xtype: "button", text: "更名", iconCls:'icon-user',
						handler:function(){
							var signedId = payDepositPanel.getForm().findField("id").getValue();
							var tabKey = "cge"+signedId;
			     	       	var url = "/customer/signed/toChangeName?signedId="+signedId+"&1=1";
			     	        business(signedId,tabKey,"签约更名",url);
				        }
					},
					{xtype: "button", text: "收款明细", iconCls:'icon-accept',hidden:marketingSignedShou,
						handler:function(){
							var signedId = payDepositPanel.getForm().findField("id").getValue();
							var tabKey = "have_"+signedId;
			     	       	var url = "/marketing/pin/haveReceive?receivablesType=2&relationId="+signedId+"&1=1&returnBackParam="+returnBackParam;
			     	       	parent.openMenu("0",tabKey,"","收款明细",url);
				        }
					},
					{hidden:marketingSigendTuiFang,xtype: "button", text: "退房信息", iconCls:'icon-accept',
						handler:function(){
							var tabKey = "tfInfo_"+businessAbnormaId;
			     	       	var url = "/customer/signed/toCheckOutView?id="+businessAbnormaId+"&1=1";
			     	       	parent.openMenu("0",tabKey,"","签约退房信息",url);
				        }
					},
					{hidden:marketingSigendHuanFang,xtype: "button", text: "换房信息", iconCls:'icon-accept',
						handler:function(){
							var tabKey = "tfcInfo_"+subscribeId;
			     	       	var url = "/marketing/changeroom/changeDetail?id="+subscribeId+"&1=1";
			     	       	parent.openMenu("0",tabKey,"","签约换房信息",url);
				        }
					}
				]
		};
		Ext.apply(this,defaultConfigg);
		this.callParent(arguments);
	},
	buttons:[
	         {id:'retrunBtn',xtype: "button",text: "返回",handler: function(){
	        	var tabKey = "sig_"+subscribeId;
	        	parent.TabControlRemove(tabKey);
	         }}
	]
});

function business(id,tabKey,title,url){
	   Ext.Ajax.request({
	   	    url: contextPath+'/marketing/businessAbnorma/getBuinessAbnormaByBuinessId',
	   	    params: {
	   	    	buinessId: id
	   	    },
	   	    method: 'POST',
	   	    success: function(response){
	   	        var text = Ext.decode(response.responseText);
	   	        if(text.res=="1"){
	   	        	parent.openMenu("0",tabKey,"",title,url);	  
	   	        }else if(text.res=="0"){
	   	        	Ext.Msg.alert('提示', text.resMsg);
	   	        }
	   	    }
	   	});
}
